package service.impl;

import dao.UserDao;
import dao.impl.UserDaoimpl;
import pojo.User;
import service.UserService;
import utils.MailUtils;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.9.1 下午 19:31
 */
public class UserServiceimpl implements UserService {
    @Override
    public boolean getuserByname(String username) {
        UserDao userDao = new UserDaoimpl();
        User user = userDao.selectUserByName(username);
        if(user != null){
            return true;
        }
        return false;
    }

    @Override
    public void addUserget(User user) {
        UserDao userDao = new UserDaoimpl();
        userDao.addUser(user);

        //设置邮件的内容
        String emailContent = "欢迎成为会员,请<a href = 'http://localhost:8080/AdminSystem_war/user?methodName=active&code="+
                user.getCode()+"'>点击激活邮箱</a>";
        MailUtils.sendMail(user.getEmail(),emailContent,"用户邮箱激活");

    }

    @Override
    public User UserLogin(String username, String password) {
        UserDao userDao = new UserDaoimpl();
        User user = userDao.findAllByusername(username);
        return user;
    }

    @Override
    public User getCode(String code) {
        UserDao userDao = new UserDaoimpl();
        User user = userDao.selectUserByCode(code);

        //完成业务判断
        if(user == null){
            return null;
        }
        //存在,设置用户的激活状态(未激活为0,激活为1)
        user.setState(1);
        //调用用户数据接口:更新用户信息
        userDao.updateUser(user);
        return user;
    }
}
